var xhr;
var selected = new Array();//已经选中的用户
var count = 0;
function checkname(){
	var idTopic = document.getElementById("idTopic");
	var id = 0;
	var name = document.getElementById("topicname").value;
	if(name ==''){
		return;
	}
	if(idTopic != null){//修改专题
		id = parseInt(idTopic.value);
		var url = "json/forum-checkTopicName.html?topicName=" + name;
		xhr = null;
		xhr = connectXHR(url, checkresult);
	}else{//增加专题
		var url = "json/forum-checkTopicName.html?topicName=" + name ;
		xhr = null;
		xhr = connectXHR(url, checkresult);
	}
}
function checkresult(){
	if(xhr == null)return;
	if(xhr.readyState == 4 && xhr.status == 200) {
		var data = eval('(' + xhr.responseText + ')');
		
		if(data == 1){
			$( "#dialog" ).dialog( "open" );
		}else{
			document.getElementById("addtopic").submit();
		}
	}
}

function selectCharge(value){
	var users = document.getElementById("users");
	var username = users.options[users.selectedIndex].text;
	if(value == '0' || value ==0){
		return;
	}
//	alert("value : " + value + ", name = " + username);
	for(var i = 0; i < count; i++){
		if(selected[i] == value){
			alert("selected");
			return;
		}
	}
	
	var charge = document.getElementById("chargePerson");
	var html = document.createElement("div");
	html.setAttribute("id","adduser" + count);
	html.setAttribute("class","adduser");
	html.innerHTML = '<label class="username">' + username 
				+ '</label><input type="button" class="cancelbtn" value="取消版主" onclick="cancelUser('
				+ count + ');"/><input type="hidden" value="' + value + '" name="chargePerson"/>';
	charge.appendChild(html);
	
	$( "input:button", "body" ).button();
	selected[count] = value;
	count++;
}

function cancelUser(id){
	var num = Number(id);
	if(num > 0){
		selected[num] = -1;
	}
	var deleteUser = document.getElementById("adduser" + id);
	if(deleteUser != null)
		deleteUser.parentNode.removeChild(deleteUser);
}